package cn.school.demo.repository

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import cn.school.demo.pojo.Article
import cn.school.demo.pojo.Collect
import cn.school.demo.pojo.Result
import cn.school.demo.util.HttpApi
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.google.gson.reflect.TypeToken
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class ArticleRepository {
    private val httpApi: HttpApi

    init {
        val retrofit =
            Retrofit.Builder().baseUrl(DataRepository.BASEURL).addConverterFactory(
                GsonConverterFactory.create()
            ).build()
        httpApi = retrofit.create(HttpApi::class.java)
    }

    fun getArticleByHobbyName(hobbyName: String,pageNum: Int,pageSize: Int): LiveData<MutableList<Article>> {
        val responseLiveData = MutableLiveData<MutableList<Article>>()
        httpApi.getArticleByHobbyName(mapOf("hobbyName" to hobbyName,"page" to pageNum,"size" to pageSize))
            .enqueue(object : Callback<Result> {
                override fun onResponse(call: Call<Result>, response: Response<Result>) {
                    response.body()?.let {
                        if (it.success) {
                            try {
                                val map = GsonUtils.fromJson<MutableList<Article>>(
                                    it.data["article"].toString(),
                                    object : TypeToken<MutableList<Article>>() {}.type
                                )
                                responseLiveData.value = map
                            } catch (e: Error) {
                                e.printStackTrace()
                            }
                        } else {
                            LogUtils.e(it.message)
                            responseLiveData.value = mutableListOf()
                        }
                    } ?: LogUtils.e("根据标签获取文章http返回值为空！")
                }

                override fun onFailure(call: Call<Result>, t: Throwable) {
                    LogUtils.e(t)
                }

            })
        return responseLiveData
    }

    fun getArticleById(id: Int): LiveData<Article> {
        val responseLiveData = MutableLiveData<Article>()
        httpApi.getArticleById(mapOf("articleId" to id)).enqueue(object : Callback<Result> {
            override fun onResponse(call: Call<Result>, response: Response<Result>) {
                response.body()?.let {
                    if (it.success) {
                        try {
                            val article = GsonUtils.fromJson<Article>(
                                it.data["article"].toString(),
                                object : TypeToken<Article>() {}.type
                            )
                            responseLiveData.value = article
                        } catch (e: Error) {
                            e.printStackTrace()
                        }
                    } else {
                        responseLiveData.value = Article(
                            -1,
                            20193549,
                            "我是测试55585",
                            "文章",
                            "学校",
                            "<span style=\"font-family: 宋体;\">：应是整个论文总体内容的体现，要引人注目，力求简短，严格控制在</span>25<span style=\"font-family: 宋体;\">字以内。",
                            "2020-02-02",
                            "1111111111111111",
                            20
                        )
                    }
                } ?: LogUtils.e("根据ID获取文章http返回值为空！")
            }

            override fun onFailure(call: Call<Result>, t: Throwable) {
                LogUtils.e(t)
            }
        })
        return responseLiveData
    }

    fun saveArticleHitById(id: Int) {
        httpApi.saveArticleHitById(mapOf("articleId" to id)).enqueue(object : Callback<Result> {
            override fun onResponse(call: Call<Result>, response: Response<Result>) {
                response.body()?.let {
                    LogUtils.d(it.message)
                } ?: LogUtils.e("保存文章点击量http返回值为空！")
            }

            override fun onFailure(call: Call<Result>, t: Throwable) {
                LogUtils.e(t)
            }
        })
    }

    fun rotationChartArticle(): LiveData<Result> {
        val responseLiveData = MutableLiveData<Result>()
        httpApi.rotationChartArticle().enqueue(object : Callback<Result> {
            override fun onResponse(call: Call<Result>, response: Response<Result>) {
                response.body()?.let {
                    responseLiveData.value = it
                } ?: LogUtils.e("获取轮播图http返回值为空！")
            }
            override fun onFailure(call: Call<Result>, t: Throwable) {
                LogUtils.e(t)
            }

        })
        return responseLiveData
    }

}